
Ext.define('FrontSuite.shared.view.AppRegistrationPanel', {
    extend: 'Ext.form.Panel',            // defines the superclass. results in class inheritance.
    alias:'widget.registrationpanel',    // creates an xtype: 'registrationpanel'
    id: 'register-view',
    title: 'Register for a new account',
    floating: true,
    centered: true,
    modal: true,
    bodyPadding: 5,
    width: 350,

    // The form will submit an AJAX request to this URL when submitted
    url: 'ff-auth.php?op=register',

    // Fields will be arranged vertically, stretched to full width
    layout: 'anchor',
    defaults: {
        anchor: '100%'
    },

    // The fields
    defaultType: 'textfield',
    items: [{
        fieldLabel: 'First Name',
        name: 'first_name',
        allowBlank: false
    },{
        fieldLabel: 'Last Name',
        name: 'last_name',
        allowBlank: false
    },{
        fieldLabel: 'Nick Name (how you wish to be called)',
        name: 'nick_name',
        allowBlank: false
    },{
        fieldLabel: 'Username (only letters and numbers)',
        name: 'username',
        allowBlank: false
    },{
        fieldLabel: 'Email Address',
        name: 'email',
        inputType: 'email',
        allowBlank: false
    },{
        fieldLabel: 'Email Address (again, to confirm)',
        name: 'email2',
        inputType: 'email',
        allowBlank: false
    },{
        fieldLabel: 'Password',
        name: 'p',
        inputType: 'password',
        allowBlank: false
    },{
        fieldLabel: 'Password (again, to confirm)',
        name: 'p2',
        inputType: 'password',
        allowBlank: false
    },{
        xtype: 'datefield',
        fieldLabel: 'Birth Date',
        name: 'birthDate',
        allowBlank: false
    },{
        fieldLabel: 'Cell Phone',
        name: 'cellphone',
        allowBlank: true
    },{
        xtype: 'hiddenfield',
        fieldLabel: 'Encrypted Password Hash',
        name: 'encryptedPasswordHash',
        allowBlank: true
    }],

    // Reset and Submit buttons
    buttons: [{
        text: 'Cancel',
        handler: function() {
            Ext.getCmp('register-view').hide();
        }
    }, {
    //    text: 'Reset',
    //    handler: function() {
    //        this.up('form').getForm().reset();
    //    }
    //}, {
        text: 'Submit',
        formBind: true, //only enabled once the form is valid
        disabled: true,
        handler: function() {
            var form = this.up('form').getForm();
console.log(form);
            if (form.isValid()) {

// first_name:Stephen
// last_name:Adkins
// nick_name:Stevo
// username:spadkins
// email:spadkins@gmail.com
// email2:spadkins@gmail.com
// p:foo
// p2:foo
// birthDate:10/08/1962
// cellphone:7706393267
// encryptedPasswordHash:

                var i, name, value;
                var values = new Object;
                for (i = 0; i < form.items.length;  i++) {
                    name = form.items[i].getName();
                    value = form.items[i].getValue();
                    values[name] = value;
                    if (name == 'p' || name == 'p2') {
                        form.items[i].setValue('');
                    }
                    else if (name == 'encryptedPasswordHash') {
                        var passwordHash = FrontSuite.makePasswordHash(algorithm, algorithmOption, password, userSalt);
                        FrontSuite.encrypt(FrontSuite.connection.clientConnectionToken, passwordHash);
                        form.items[i].setValue('');
                    }
                }

                form.submit({
                    success: function(form, action) {
                       Ext.Msg.alert('Success', action.result.msg);
                    },
                    failure: function(form, action) {
                        Ext.Msg.alert('Failed', action.result.msg);
                    }
                });
            }
        }
    }]
});

